﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataControl;
using System.Data;
using System.Text;

namespace ATW.MT.BUS
{
    public class bus_T_AppUserAuthorised
    {
        DataAccess da = null;
        public bus_T_AppUserAuthorised()
        {
            da = new DataAccess();
        }
        #region 方法

        //查user表
        public DataTable AppUser(string name, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from T_AppUser where( tauType='1' or tauType='2') ");
            if (name != "")
            {
                sql.Append(" and tauLoginId like '%" + name + "%'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "TAUID");
        }
        /// <summary>
        /// 查询user表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable AppUser(string name, string type, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from T_AppUser where( tauType='1' or tauType='2') ");
            if (name != "")
            {
                sql.Append(" and tauLoginId like '%" + name + "%'");
            }
            if (type != "")
            {
                sql.Append(" and tauType = '" + type + "'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "TAUID");
        }
        //查授权表
        public DataTable InformationCategory()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from T_InformationCategory where (ticRowStatus=0 or ticRowStatus='0')");
            return da.ExecuteDataTable(sql.ToString());
        }
        //查T_AppUserAuthorised表
        public DataTable AppUserAuthorised(string strId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from  T_AppUserAuthorised where tauaRowStatus=0 and tauaTauId='" + strId + "'");
            return da.ExecuteDataTable(sql.ToString());
        }
        //修改认证项目
        public int Change(string tauaTauId, string tauaTicId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from  T_AppUserAuthorised where tauaTauId={0} and tauaTicId={1}", tauaTauId, tauaTicId);
            var dt = da.ExecuteDataTable(sql.ToString());
            StringBuilder sql2 = new StringBuilder();
            if (dt.Rows.Count > 0)
            {
                sql2.AppendFormat(@"update T_AppUserAuthorised set tauaRowStatus=0,UpdatedDate=getdate() where tauaTauId={0} and tauaTicId={1}", tauaTauId, tauaTicId);
            }
            else
            {
                sql2.AppendFormat(@"insert into T_AppUserAuthorised(tauaTauId,tauaTicId,tauaRowStatus,CreatedDate) values('{0}','{1}','0',getdate())", tauaTauId, tauaTicId);
            }
            return da.ExcuteNoQuery(sql2.ToString());
        }
        //认证
        public int add(string tauaTauId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_AppUser set tauIsAuthorised=1 where TAUID={0} ", tauaTauId);
            da.ExcuteNoQuery(sql.ToString());
            StringBuilder sql1 = new StringBuilder();
            sql1.AppendFormat(@"update T_AppUserAuthorised set tauaRowStatus=2 where tauaTauId={0}", tauaTauId);
            return da.ExcuteNoQuery(sql1.ToString());
        }
        //取消认证
        public int delete(string tauaTauId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_AppUserAuthorised set tauaRowStatus=2,UpdatedDate=getdate()  where tauaTauId={0}",
                tauaTauId);
            sql.AppendFormat(@"update T_AppUser set tauIsAuthorised=0,UpdatedDate=getdate()  where TAUID={0}", tauaTauId);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <param name="trueName"></param>
        /// <returns></returns>
        public int AddUser(string account, string pwd, string trueName)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into [T_AppUser] ([tauType],[tauLoginId],[tauName],[tauPsw])values('2','{0}','{1}','{2}')",
                account, trueName, pwd);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 账号是否存在
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public bool isUserExist(string account)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_appuser where tauloginid='" + account + "'");
            DataTable dt = da.ExecuteDataTable(sql.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        #endregion
    }
}